package com.itxx.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpRecevie {
    public static void main(String[] args) throws IOException {
//Socket类负责数据接收
//tcp服务器的Socket用ServerSocket监听得来的->Socket类对数据进行接收

        //1.创建ServerSocket对象
        ServerSocket ss = new ServerSocket(54321);

       /*
       2.监听客户端连接,返回Socket对象
       accept()是一个阻塞方法
       */
        Socket accept = ss.accept();
        //3.获取输入流对象 读数据 并将数据显示在控制台
        InputStream is = accept.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        System.out.println("接收的数据为"+s);

        //4.释放资源
        accept.close();

    }
}
